package com.igeekhome.interceptor;

import com.igeekhome.pojo.SysStaff;
import com.igeekhome.util.CommonUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 立以宁
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getRequestURI().contains("login") || request.getRequestURI().contains("loginCheck") || CommonUtil.releaseSuffix(request)) {
            return true;
        }
        SysStaff staff = (SysStaff) request.getSession().getAttribute("staff");

        if (request.getRequestURI().contains("api")) {
            response.setContentType("application/json;charset=utf-8");
            if ("".equals(staff) || staff == null) {
                request.setAttribute("msg", "没有权限登录");
                response.sendRedirect(request.getContextPath()+"/page/login");
            } else {
                //放行
                return true;
            }
        } else {
            if (staff == null) {
                //没有登录权限，返回登录界面
                response.sendRedirect(request.getContextPath()+"/page/login");
            } else {
                return true;
            }
        }

        return false;
    }


}
